package cn.zb.zbrhback.org.controller;

import cn.zb.zbrhback.auth.annotation.ZBPermission;
import cn.zb.zbrhback.basic.util.AjaxResult;
import cn.zb.zbrhback.org.domain.PageQuery;
import cn.zb.zbrhback.basic.util.PageResult;
import cn.zb.zbrhback.org.domain.Department;
import cn.zb.zbrhback.org.service.IDepartmentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(value = "部门Api",tags = {"部门操作接口"})
@RestController
@RequestMapping("/department")
@ZBPermission(name = "部门管理",desc = "部门管理的权限")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 新增或修改
     * */
    @ApiOperation(value = "新增或修改接口",notes = "有id为修改，否则为新增")
    @PutMapping
    @ZBPermission(name = "新增或修改",desc = "新增或修改")
    public AjaxResult addorupdate(@RequestBody Department department){
        try {
            System.out.println("数据"+department);
            if(Objects.nonNull(department.getId())){
                departmentService.update(department);
            }else {
                departmentService.add(department);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("编辑失败"+e.getMessage());
        }
    }

    /**
     * 根据id删除
     * */
    @ApiOperation(value = "删除接口",notes = "根据主键id删除")
    @DeleteMapping("/{id}")
    @ZBPermission(name = "根据id删除",desc = "根据id删除")
    public AjaxResult deleteById(@ApiParam(name = "id",value = "删除的主键必须",required = true) @PathVariable("id")Long id){
        try {
            departmentService.deleteById(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }

    /**
     * 根据id查询
     * */
    @ApiOperation(value = "单个查询接口",notes = "根据id查询")
    @GetMapping("/{id}")
    @ZBPermission(name = "根据id查询",desc = "根据id查询")
    public AjaxResult findById(@ApiParam(name = "id",value = "查询的主键必须",required = true) @PathVariable("id")Long id){
        try {
            Department department = departmentService.getById(id);
            return AjaxResult.me().setData(department);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /**
     * 查询所有
     * */
    @ApiOperation(value = "查询所有接口",notes = "不需要任何参数")
    @GetMapping
    @ZBPermission(name = "查询所有",desc = "查询所有")
    public AjaxResult findAll(){
        try {
            List<Department> departments = departmentService.getAll();
            return AjaxResult.me().setData(departments);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /**
     * 分页条件查询
     * */
    @ZBPermission(name = "分页条件查询",desc = "分页条件查询")
    @ApiOperation(value = "分页条件查询接口",notes = "需要参数当前页以及每页显示条数，条件可有可无")
    @PostMapping
    public AjaxResult findPage(@RequestBody PageQuery pageQuery){
        try {
            PageResult pagemsg = departmentService.findPage(pageQuery);
            return AjaxResult.me().setData(pagemsg);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页条件查询失败"+e.getMessage());
        }
    }

    @ApiOperation(value = "批量删除接口",notes = "需要参数删除的id数组")
    @PatchMapping
    @ZBPermission(name = "批量删除",desc = "批量删除接口")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页条件查询失败"+e.getMessage());
        }
    }

    @ApiOperation(value = "获取部门关系树接口",notes = "不需要任何参数")
    @GetMapping({"/tree","/a"})
    @ZBPermission(name = "获取部门关系树",desc = "获取部门关系树接口")
    public AjaxResult getDeptTree(){
        try {
            List<Department> departments = departmentService.getTree();
            return AjaxResult.me().setData(departments);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询部门树失败"+e.getMessage());
        }
    }
}
